<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
	<title>notes</title>
	<style type="text/css">
	  table
	  {
	  border-collapse: collapse;
	  }
	  table, tr, th, td
	  {
	  border: 1px solid black;
	  padding-left: 8px;
	  padding-right: 8px;
	  }
	  th, td
	  {
	  min-width: 2em;
	  }
	  pre
	  {
	  border-style: solid
	  }
	  .comment
	  {
	  color: green;
	  }
	</style>
	<link rel="stylesheet" title="plain" type="text/css" href="plain.css" />
	<link rel="alternate stylesheet" title="colour" type="text/css" href="colour.css"/>
</head>
<body>
	<h2>Notes</h2>
	<p>Plan for this class:<ol>
		<li>Review of the combat loop:<ul>
			<li>recap</li>
			<li>what was difficult?</li>
			<li>duplicated effort / code</li>
			<li>longer variables - difficult to distinguish</li>
			<li>review the high level structure of the combat loop:</li>
			<pre>

     bool player_turn = ...;

     do {

          if (player_turn) {

               ...

          } else {

               ...
          }

          player_turn = !player_turn;
     
     } while (player_hit_points > 0 && dragon_hit_points > 0);
			</pre>
		</ul></li>
		<li>data structures<ul>
			<li>not the same as structured programming</li>
			<li>new ways to look at data</li>
			<li>arrays of data types<ul>
			    <li>allocate and assign:</li>
			    <pre>

     int character_AC[9];

     character_AC[0] = 17;
     character_AC[1] = 17;
     character_AC[2] = 17;
     character_AC[3] = 17;
     character_AC[4] = 15;
     character_AC[5] = 20;
     character_AC[6] = 17;
     character_AC[7] = 20;
     character_AC[8] = 20;
			    </pre>
			    <li>enumerations automatically number things</li>
			    <li>useful as a named index into an array:</li>
			    <pre>

     enum {
          Frodo,
          Sam,
          Merry,
          Pippin,
          Gandalf,
          Gimli,
          Legolas,
          Aragorn,
          Boromir
     };
			    </pre>
			    <li>combining the two:</li>
			    <pre>

     int character_AC[9];

     character_AC[Frodo]   = 17;
     character_AC[Sam]     = 17;
     character_AC[Merry]   = 17;
     character_AC[Pippin]  = 17;
     character_AC[Gandalf] = 15;
     character_AC[Gimli]   = 20;
     character_AC[Legolas] = 17;
     character_AC[Aragorn] = 20;
     character_AC[Boromir] = 20;
			    </pre>
			    <li>notice the OCD in code alignment</li>
			    <li>an alternative array initialisation:</li>
			    <pre>

     int character_AC[9] = { 17, 17, 17, 17, 15, 20, 17, 20, 20 };
			    </pre>
			    <li>we can also build arrays in 2 or more dimensions:</li>
			    <pre>

     enum {
          AC,
          fortitude,
          reflex,
          will,
     };

     int defences[9][4] = {
     <span class="comment">//     AC  FT  RF  WL</span>
          { 17, 12, 17, 11 }, <span class="comment">// Frodo</span>
          { 17, 12, 17, 11 }, <span class="comment">// Sam</span>
          { 17, 12, 17, 11 }, <span class="comment">// Merry</span>
          { 17, 12, 17, 11 }, <span class="comment">// Pippin</span>
          { 15, 12, 15, 14 }, <span class="comment">// Gandalf</span>
          { 20, 17, 12, 10 }, <span class="comment">// Gimli</span>
          { 17, 11, 16, 13 }, <span class="comment">// Legolas</span>
          { 20, 17, 14, 16 }, <span class="comment">// Aragorn</span>
          { 20, 18, 13, 11 }  <span class="comment">// Boromir</span>
     };

			    </pre>
			    <li>you can write code by index:</li>
			    <pre>

     if (attack > defences[player][type]) {

          ...
     }
			    </pre>
			    <li>either way up:</li>
			    <pre>

     int defences[4][9] = {
     <span class="comment">//     FR  SM  MR  PP  GD  GM  LG  AR  BR</span>
          { 17, 17, 17, 17, 15, 20, 17, 20, 20 }, <span class="comment">// AC</span>
          { 12, 12, 12, 12, 12, 17, 11, 17, 18 }, <span class="comment">// fortitude</span>
          { 17, 17, 17, 17, 15, 12, 16, 14, 13 }, <span class="comment">// reflex</span>
          { 11, 11, 11, 11, 14, 10, 13, 16, 11 }  <span class="comment">// will</span>
     };

     if (attack > defences[type][player]) {

          ...
     }
			    </pre>
			</ul></li>
			<li>composite data types<ul>
			    <li>like a database record or form</li>
			    <li>simply list the fields (type and name)</li>
			    <pre>

     struct character {

          const char * name;
          int attack_bonus;
          int damage_dice;
          int damage_bonus;
          int defences[4];
          bool is_player;
          int hit_points;
     };

			    </pre>
			    <li>character now a type:</li>
			    <pre>

     character Frodo;

     Frodo.name                = "Frodo";
     Frodo.attack_bonus        = 5;
     Frodo.damage_dice         = 6;
     Frodo.damage_bonus        = 2;
     Frodo.defences[AC]        = 17;
     Frodo.defences[fortitude] = 12;
     Frodo.defences[reflex]    = 17;
     Frodo.defences[will]      = 11;
     Frodo.is_player           = true;
     Frodo.hit_points          = 20;
			    </pre>
			    <li>reference by value</li>
			    <li>use dot syntax to reference</li>
			    <li>can still be initialised inline</li>
			    <li>order determines binding:</li>
			    <pre>

     character Frodo = { "Frodo", 5, 6, 2, {17, 12, 17, 11}, true, 20};
			    </pre>
			    <li>reference by pointer</li>
			    <li>use &amp; operator to take address of value</li>
			    <pre>

     int x = 4;
     int * p = &amp;x;

     int * q = &amp;4; // error!

     character Player;
     character * attacker = &Player;
			    </pre>
			    <li>allows abstraction:</li>
			    <pre>

     character Player;
     character Dragon;

     character * attacker = &Player;
     character * defender = &Dragon;

     do {

          if (rand() % 20 + 1 + attacker->attack_bonus &gt;= defender-&gt;defences[AC]) {

               defender-&gt;hit_points -= rand() % attacker-&gt;damage_dice + 1 + attacker-&gt;damage_bonus;
          }

          character * temporary = attacker;
          attacker = defender;
          defender = temporary;


     } while (attacker-&gt;hit_points &gt; 0 &amp;&amp; defender-&gt;hit_points &gt; 0);
			    </pre>
			    <li>use -&gt; syntax to reference</li>
			</ul></li>
			<li>arrays of composite data types</li>
			<pre>

     character fellowship[9];
			</pre>
			<li>composite of arrays of data</li>
			<pre>

     struct fellowship {

          const char * name[9];
          int attack_bonus[9];
          int damage_dice[9];
          ...
     };
			</pre>
		</ul></li>
	  </ol>
	</p>
</body>
</html>
